--File ： ChapterView.lua
--Author : zee
--Date   : 2015/4/8
--Description : 关卡主界面 chapter.fla 第一帧



TouchObj = class("TouchObj",function()
    -- 继承
    local ret = {} 

    -- 纪录的 cocos 的 touch 对象
    ret.touch = nil;
    -- 每次触摸开始时的启始位置
    ret.startPoi = cc.p(0,0);
    -- 当前位置
    ret.nowPoi = cc.p(0,0);
    -- 是否移动过
    ret.moved = false;
    -- 手势结束的标志。
    ret.willEnd = false;
    -- 距离起始位置的距离。
    ret.distance = 0;
    -- 总共滑动过了多少距离。
    ret.road = 0;

    return ret
end)
TouchObj.__index = TouchObj 

function TouchObj:create()
    local ret = TouchObj:new()
    return ret
end 

function TouchObj:reset()
    self.touch = nil;
    -- 每次触摸开始时的启始位置
    self.startPoi = cc.p(0,0);
    -- 当前位置
    self.nowPoi = cc.p(0,0);
    -- 是否移动过
    self.moved = false;
    -- 手势结束的标志。
    self.willEnd = false;
    -- 距离起始位置的距离。
    self.distance = 0;
    -- 总共滑动过了多少距离。
    self.road = 0;
end

function TouchObj:setTouch( t )
    if( t==nil ) then 
        self:reset()
        return;
    end
    if( self.touch==nil) then
        self.touch = t
        self.startPoi = t:getLocation();
    end
    if self.touch~=t then 
        return
    end
    self.nowPoi = self.touch:getLocation();
    self.distance = distance(self.startPoi, self.nowPoi);
    self.road = 0;
    if ( self.distance>=G_Touch_Move_Distance ) then 
        self.moved = true
    end
end


function TouchObj:isNull()
    return self.touch == nil
end

function TouchObj:isTouch( t )
    if( t==nil or self.touch==nil) then 
        return false
    end
    return self.touch == t
end


-------------------- ******************************** ------------------------------------
--          
-------------------- ******************************** ------------------------------------
TouchCenter = class("TouchCenter",function()
    -- 继承
    local ret = {} 

    -- 正在响应的touch 数量
    ret.count = 0;
    -- 两个 touchObj
    ret.ary_touch = {};
    -- 操作状态
    ret.touchMax = Touch_None;
    -- 操作类型
    ret.touchType = Touch_None;
    
    return ret
end)

TouchCenter.__index = TouchCenter
TouchCenter.__instance = nil



--------------------------------------------------------
-- 初始化
--------------------------------------------------------
function TouchCenter:getInstance()
    if TouchCenter.__instance==nil then 
        TouchCenter.__instance = TouchCenter:create()
    end
    return TouchCenter.__instance
end 


function TouchCenter:create()
    TouchCenter.__instance = TouchCenter:new()
    TouchCenter.__instance:init()
    return TouchCenter.__instance
end 


function TouchCenter:init()
    self.ary_touch = {}
    self.ary_touch[1] = TouchObj:create()
    self.ary_touch[2] = TouchObj:create()
end


function TouchCenter:getTouchCount()
    local a = 0
    if (self.ary_touch[1]:isNull()==false) then 
        a = a+1
    end
    if (self.ary_touch[2]:isNull()==false) then 
        a = a+1
    end
    return a
end



--------------------------------------------------------
-- 手势事件
--------------------------------------------------------
function TouchCenter:onTouchBegan(touches, event)
    for k,t in pairs(touches) do 
        if( self.ary_touch[1]:isNull()==true ) then 
            self.ary_touch[1]:setTouch(t)
        elseif ( self.ary_touch[2]:isNull()==true )  then 
            self.ary_touch[2]:setTouch(t)
        end
    end
    self:checkTouchMax()
end


function TouchCenter:onTouchMoved(touches, event)
    for k,t in pairs(touches) do 
        if( self.ary_touch[1]:isTouch(t) ) then
            self.ary_touch[1]:setTouch(t)
        end
        if( self.ary_touch[2]:isTouch(t) ) then 
            self.ary_touch[2]:setTouch(t)
        end
    end
    -- 移动时就要检查状态。
    self:checkTouchTypeInMoving()
end


function TouchCenter:onTouchEnd(touches, event)
    -- 先设置即将结束标记。
    for k,t in pairs(touches) do 
        if(self.ary_touch[1]:isTouch(t)==true) then
            self.ary_touch[1].willEnd = true
        elseif (self.ary_touch[2]:isTouch(t)==true )then
            self.ary_touch[2].willEnd = true
        end
    end
    -- 检查触摸结果
    self:checkTouchType()
    -- 检查完后，清空
    for k,t in pairs(touches) do 
        if(self.ary_touch[1]:isTouch(t)==true) then
            self.ary_touch[1]:reset()
        end
        if (self.ary_touch[2]:isTouch(t)==true )then
            self.ary_touch[2]:reset()
        end
    end
    -- 结束时，如果所有手抖离开了才算一个手势周期结束。。
    if ( self:getTouchCount()==0) then 
        self.touchMax = 0
        self.touchType = Touch_None
        cclook("手势恢复0状态。\n\n\n\n")
    end
end


-----------------  立即检查
function TouchCenter:checkTouchMax()
    self.count = self:getTouchCount()
    -- 先判断最大触摸数
    if (self.count==1 and self.touchMax<2 ) then 
        self.touchMax = 1
    elseif ( self.count==2 ) then 
        self.touchMax = 2
    end
end


function TouchCenter:checkTouchTypeInMoving()
    -- 
    self:checkTouchMax()
    --  在根据最大触摸判断手势类型。(滑动时，单手的暂且不定性触摸类型 )
    if (self.touchMax==1 ) then 
        self.touchType = Touch_None

    elseif( self.touchMax==Touch_Two ) then
        if( self.ary_touch[1].moved==false and self.ary_touch[2].moved==false ) then 
            self.touchType = Touch_Two_Click

        elseif( self.ary_touch[1].moved==true and self.ary_touch[2].moved==false ) then 
            self.touchType = Touch_Two_M_Click

        elseif( self.ary_touch[1].moved==false and self.ary_touch[2].moved ) then 
            self.touchType = Touch_Two_C_Moving

        elseif( self.ary_touch[1].moved and self.ary_touch[2].moved ) then 
            self.touchType = Touch_Two_Moving
            self:onTouchEvent_moving()
        end
    end
end


function TouchCenter:checkTouchType()
    self.count = self:getTouchCount()
    cclook(" 检查到: touchMax : %s ", self.touchMax )
    if ( self.touchMax==1) then 
        if( self.ary_touch[1].moved==false ) then 
            cclook(" 1 = [ 单机 ]");
            self.touchType = Touch_One_Click
            self:onTouchEvent_end()

        elseif ( self.ary_touch[1].moved==true and self.ary_touch[1].distance>G_Touch_Move_Distance*2 ) then 
            cclook(" 2 = [ 滑动 ]");
            self.touchType = Touch_One_Move
            self:onTouchEvent_end()
        end

    elseif( self.count==2 and self.touchMax==2 ) then 
        if( self.ary_touch[1].moved==false and self.ary_touch[2].moved==false ) then 
            cclook(" 4 = [ 双指头点击 ]");
            self.touchType = Touch_Two_Click;
            self:onTouchEvent_end()
            -- 双点事件完后，要把第二个
            self.ary_touch[1]:reset()
            self.ary_touch[1]:setTouch( self.ary_touch[2].touch )
            self.ary_touch[2]:reset()

        elseif( self.ary_touch[1].moved==true and self.ary_touch[2].moved==false ) then 
            cclook(" 5 = [ 滑动后被点停 ]");
            self.touchType = Touch_Two_M_Click;
            self:onTouchEvent_end()
            self.ary_touch[1]:reset();
            self.ary_touch[2]:reset();

        elseif( self.ary_touch[1].moved==false and self.ary_touch[2].moved ) then 
            cclook(" 6 = [ 先按压后滑动 ]");
            self.touchType = Touch_Two_C_Moving;
            self:onTouchEvent_end()
            

        elseif( self.ary_touch[1].moved and self.ary_touch[2].moved ) then 
            cclook(" 7 = [ 双手滑动结束 ]");
            self.touchType = Touch_Two_Moving;
            self:onTouchEvent_end()
            self.ary_touch[1]:reset();
            self.ary_touch[2]:reset();
        end
    end

    --]]
end


function TouchCenter:onFrame(dt)

end



--------------------------------------------------------
-- 手势事件触发
--------------------------------------------------------
function TouchCenter:onTouchEvent_began()

end

function TouchCenter:onTouchEvent_moving()
    if ( self.touchType==Touch_Two_Moving ) then 
        BattleLayer:getInstance().hero:bigArrowStart( self.ary_touch[1].nowPoi, self.ary_touch[2].nowPoi )        
        BattleLayer:getInstance().hero:bigArrowMove( self.ary_touch[1].nowPoi, self.ary_touch[2].nowPoi )
    end
end


function TouchCenter:onTouchEvent_end()
    -- 单点指向移动目标。
    if ( self.touchType==Touch_One_Click ) then 
        BattleLayer:getInstance().hero:moveToMap( self.ary_touch[1].nowPoi )

    -- 滑动 快速滚向相对目标。
    elseif ( self.touchType==Touch_One_Move ) then 
        BattleLayer:getInstance().hero:rushToMap( self.ary_touch[1].startPoi, self.ary_touch[1].nowPoi )

    -- 两个手指同时在屏幕上的次序点击。
    elseif ( self.touchType==Touch_Two_Click ) then 
        BattleLayer:getInstance().hero:jumpToMap( self.ary_touch[2].nowPoi )

    --- 单个手指按压时，另一个手指滑动
    elseif ( self.touchType==Touch_Two_C_Moving ) then 
        BattleLayer:getInstance().hero:fire_arrow( self.ary_touch[2].nowPoi )

    -- 双指移动，射出大箭
    elseif ( self.touchType==Touch_Two_Moving ) then 
        BattleLayer:getInstance().hero:fire_bigArrow( self.ary_touch[1].nowPoi, self.ary_touch[2].nowPoi )

    -- 先滑后点，移动中放出大招。
    elseif ( self.touchType==Touch_Two_M_Click ) then
        BattleLayer:getInstance().hero:fire_bigSkill( 
            self.ary_touch[1].startPoi, 
            self.ary_touch[1].nowPoi ) 

    end
end











